#!/usr/bin/perl
#change_bond.cgi

require './network-lib.pl';
&ReadParse();
local $a;
local @actives;
#Network Load Balancing Page
=xxx
if($in{'nettype'} eq "NLB")
{
my $b = &get_interface(bond0);
&header($text{'edit_interface_title'}, "", "changebonding", 0, 1);
print "<form action=save_interface.cgi method=post onsubmit='document.forms[0].savebtn.disabled = true;'>\n";
print "<input type=hidden name=nettype value='$in{'nettype'}'>\n";
print "<input type=hidden name=eth value='bond0'>\n";
print "<table class=mode1 border=0 cellspacing=0 cellpadding=4>\n";
print "<tr><td><b>$text{'ifcs_name'}</b></td>\n";
print "<td>", &get_adaptername($b->{'irq'}), "</td> </tr>\n";
print "<tr><td><b>$text{'ifcs_ip'}</b></td>\n";
printf "<td><input type=hidden name=iprdn value='static'><input class=text name=ipin value='$b->{'address'}'></td> </tr>\n";
print "<tr><td><b>$text{'ifcs_mask'}</b></td>\n";
print "<td><input class=text name=maskin size=25 value='$b->{'netmask'}'></td> </tr>\n";
print "<tr><td><b>$text{'ifcs_broadcast'}</b></td>\n";
print "<td><input class=text name=broadcastin size=25 value='$b->{'broadcast'}'></td> </tr>\n";
print "<tr><td><b>$text{'ifcs_gateway'}</b></td>\n";
print "<td><input class=text name=gatewayin size=25 value='$b->{'gateway'}'></td> </tr>\n";

print "<tr><td colspan=2><input class=button1 type=submit name=savebtn value='$text{'submit'}'></td></tr>\n";
print "</table></form>\n";
&footer("index.cgi", $text{'interface_return'});
} 
=cut
# Fault Tolarence Page
if($in{'nettype'} ne "NB")
{
if(-e "/var/lock/apple.lock")
{
	&error($text{'appleison'});
}

my $b = &get_interface(bond0);
&header($text{'change_bonding_title'}, "", "changebonding", 0, 1);

print "<form action=save_interface.cgi method=post onsubmit='document.forms[0].savebtn.disabled = true;'>\n";
print "<input type=hidden name=nettype value='$in{'nettype'}'>\n";
print "<input type=hidden name=eth value='bond0'>\n";
print "<table class=mode1 border=0 cellspacing=0 cellpadding=4>\n";
print "<tr><td><b>$text{'ifcs_name'}</b></td>\n";
print "<td>", &get_adaptername($b->{'irq'}), "</td> </tr>\n";

print "<tr><td><b>$text{'ifcs_ip'}</b></td>\n";
printf "<td><input type=hidden name=iprdn value='static'><input class=text name=ipin value='$b->{'address'}'></td> </tr>\n";
print "<tr><td><b>$text{'ifcs_mask'}</b></td>\n";
print "<td><input class=text name=maskin size=25 value='$b->{'netmask'}'></td> </tr>\n";
#print "<tr><td><b>$text{'ifcs_broadcast'}</b></td>\n";
#print "<td><input class=text name=broadcastin size=25 value='$b->{'broadcast'}'></td> </tr>\n";
#print "<tr><td><b>$text{'ifcs_gateway'}</b></td>\n";
#print "<td><input class=text name=gatewayin size=25 value='$b->{'gateway'}'></td> </tr>\n";

print "<tr><td><b>$text{'join_card'}</b></td>\n";
my @joincard = ();
my @availablecard = ();
my $i;
open(LOCALGROUP, "/etc/bond.conf");
@joincard=<LOCALGROUP>;
close(LOCALGROUP);

	print "<td>\n";
	print "<table border=0 class=mode1>";
	print "<tr><td>$text{'already_bond'}</td><td></td><td>$text{'available_to_bond'}</td></tr>";
	print "<tr><td>\n";
	print "<select class=list2 name=localgrouplist size=7 multiple>\n";
	$i = 0;
	while($i < @joincard)
	{
		chomp($joincard[$i]);
		if($joincard[$i] =~ /eth/){
		print "<option value=\'$joincard[$i]\'>$joincard[$i]\n";
		}
		$i++;
	}
	print "</select>\n";
	print "<input name=listinput type=hidden value = ''>";
	print "</td><td>";
	print "<table><tr><td><input class=sbutton type=button onclick='addallselect();' value=\'$text{'bonding_button_addall'}\'></td></tr>\n";
	print "<tr><td><input class=sbutton type=button onclick='removeallselect();' value=\'$text{'bonding_button_removeall'}\'></td></tr>\n";
	print "<tr><td><input class=sbutton type=button onclick='addselect();' value=\'$text{'bonding_button_add'}\'></td></tr>\n";
	print "<tr><td><input class=sbutton type=button onclick='removeselect();' value=\'$text{'bonding_button_remove'}\'></td></tr>\n";
	print "</table>\n";
	print "</td><td>\n";
	print "<select class=list2 name=localgrouplist2 size=7 multiple>\n";
	@availablecard = &active_interfaces;
	my $a;
	foreach $a (@availablecard) {
		if($a->{'name'} =~ /eth/)
		{
			print "<option value=\'$a->{'name'}\'>$a->{'name'}\n";
		}
	}
	print "</select>\n";
	print "</td></tr></table>\n";
	print "</td></tr>\n";

print "<tr><td colspan=2><input class=button1 type=submit name=savebtn value='$text{'submit'}' onclick='checkselect();'></td></tr>\n";
print "</table>\n";
print "</form>\n";
print <<EOF;
<script>
function find_item_in_grouplist(text1)
{
	var rt = 0;
	var ii;
	for(ii = 0; ii < document.forms[0].localgrouplist.options.length; ii++)
	{
		if(document.forms[0].localgrouplist.options[ii].text == text1)
		{
			rt = 1;
		}
	}
	return rt;
}

function addallselect()
{
	for(i = document.forms[0].localgrouplist.options.length; i > 0; i--)
	{
		document.forms[0].localgrouplist.options[i - 1] = null;
	}

	for(i = 0; i < document.forms[0].localgrouplist2.options.length; i++)
	{
		var text1 = document.forms[0].localgrouplist2.options[i].text;
		var option1 = new Option(text1,text1);
		document.forms[0].localgrouplist.options[i] = option1;
	}

	if(document.forms[0].localgrouplist.options.length > 0)
	{
		document.forms[0].listinput.value = document.forms[0].localgrouplist.options[0].text;
	}
	else
	{
		document.forms[0].listinput.value = "";
	}

	for(i = 1; i < document.forms[0].localgrouplist.options.length; i++)
	{
		document.forms[0].listinput.value += " " + document.forms[0].localgrouplist.options[i].text;
	}
}

function removeallselect()
{
	for(i = document.forms[0].localgrouplist.options.length; i > 0; i--)
	{
		document.forms[0].localgrouplist.options[i - 1] = null;
	}

	document.forms[0].listinput.value = "";
}

function addselect()
{
	var j= document.forms[0].localgrouplist.options.length;

	for(i = document.forms[0].localgrouplist2.options.length; i > 0; i--)
	{
		if(document.forms[0].localgrouplist2.options[i - 1].selected)
		{

			if(find_item_in_grouplist(document.forms[0].localgrouplist2.options[i - 1].text) == 0)
			{
				document.forms[0].localgrouplist.options[j] = new Option(document.forms[0].localgrouplist2.options[i - 1].text, document.forms[0].localgrouplist2.options[i - 1].text);
				j = document.forms[0].localgrouplist.options.length;
			}
		}
	}

	if(document.forms[0].localgrouplist.options.length > 0)
	{
		document.forms[0].listinput.value = document.forms[0].localgrouplist.options[0].text;
	}
	else
	{
		document.forms[0].listinput.value = "";
	}

	for(i = 1; i < document.forms[0].localgrouplist.options.length; i++)
	{
		document.forms[0].listinput.value += " " + document.forms[0].localgrouplist.options[i].text;
	}
}

function removeselect()
{
	for(i = document.forms[0].localgrouplist.options.length; i > 0; i--)
	{
		if(document.forms[0].localgrouplist.options[i - 1].selected)
		{
			document.forms[0].localgrouplist.options[i - 1] = null;
		}
	}

	if(document.forms[0].localgrouplist.options.length > 0)
	{
		document.forms[0].listinput.value = document.forms[0].localgrouplist.options[0].text;
	}
	else
	{
		document.forms[0].listinput.value = "";
	}

	for(i = 1; i < document.forms[0].localgrouplist.options.length; i++)
	{
		document.forms[0].listinput.value += " " + document.forms[0].localgrouplist.options[i].text;
	}
	//history.go(0);
}
function checkselect()
{
	if(document.forms[0].listinput.value == "")
	{
		for(i = 0; i < document.forms[0].localgrouplist.options.length; i++)
		{
			document.forms[0].listinput.value += " " + document.forms[0].localgrouplist.options[i].text;
		}
	}
}
</script>
EOF
&footer("index.cgi", $text{'interface_return'});
}
#No Bonding Page
elsif($in{'nettype'} eq "NB")
{
	`/etc/sysconfig/network-scripts/bond_cfg stop`;
	&redirect("index.cgi");
}
